/*
 * Copyright 2008 Slava Chernyak
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package com.chernyak.DataAdapter.types;

import com.chernyak.DataAdapter.CompilerException;

public final class TypeDeclFactory {
	private TypeDeclFactory() {}
	
	public static TypeDecl getTypeDecl(String type, String name, boolean compare, int ln) {
		return getTypeDecl(type, name, compare, 0, ln);
	}
	
	public static TypeDecl getTypeDecl(String type, String name, int arrayDims, int ln) {
		return getTypeDecl(type, name, false, arrayDims, ln);
	}
		
	private static TypeDecl getTypeDecl(
	    String type, String name, boolean compare, int arrayDims, int ln) {	
		TypeDecl baseDecl;
		
		// primitives
		if ("boolean".equals(type)) 
			baseDecl =  new BooleanDecl(name, compare, ln);
		else if ("byte".equals(type)) 
			baseDecl =  new ByteDecl(name, compare, ln);
		else if ("char".equals(type)) 
			baseDecl =  new CharDecl(name, compare, ln);
		else if ("double".equals(type)) 
			baseDecl =  new DoubleDecl(name, compare, ln);
		else if ("float".equals(type)) 
			baseDecl =  new FloatDecl(name, compare, ln);
		else if ("int".equals(type)) 
			baseDecl =  new IntDecl(name, compare, ln);
		else if ("long".equals(type)) 
			baseDecl =  new LongDecl(name, compare, ln);
		else if ("short".equals(type)) 
			baseDecl =  new ShortDecl(name, compare, ln);
		
		// String
		else if ("String".equals(type)) 
			baseDecl =  new StringDecl(name, compare, ln);
		
		// composites
		else  {
			if (compare) {
				throw new CompilerException("Cannot compare on composite type", ln);
			}
			baseDecl = new CompositeTypeDecl(type,name,ln);
		}
		
		if (arrayDims > 0) {
			return new ArrayedTypeDecl(baseDecl, name, arrayDims, ln);
		} else {
			return baseDecl;
		}
	}
}
